package com.yuwj.chinaa.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yuwj.chinaa.R;
import com.yuwj.chinaa.adapter.CommunityLevelMemberAdapter;
import com.yuwj.chinaa.base.BaseActivity;
import com.yuwj.chinaa.entity.EntityCommunityLevel;
import com.yuwj.chinaa.entity.EntityCommunityLevelMember;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 社区推广级别成员界面
 */
public class CommunityPopularizeLevelMemberActivity extends BaseActivity {


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rv_community_level_member)
    RecyclerView rvCommunityLevelMember;
    List<EntityCommunityLevelMember> entityCommunityLevelMemberList;
    CommunityLevelMemberAdapter communityLevelMemberAdapter;
    EntityCommunityLevel entityCommunityLevel;
    IPresenter presenter;

    public static Intent createIntent(Context context, EntityCommunityLevel entityCommunityLevel) {
        Intent intent = new Intent(context, CommunityPopularizeLevelMemberActivity.class);
        intent.putExtra("entityCommunityLevel", entityCommunityLevel);
        return intent;
    }

    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.activity_community_popularize_level_member;
    }

    @Override
    public void initParam() {
        presenter = new PresenterImpl(this);
        Intent intent = getIntent();
        entityCommunityLevel = (EntityCommunityLevel) intent.getSerializableExtra("entityCommunityLevel");
        entityCommunityLevelMemberList = new ArrayList<>();
        communityLevelMemberAdapter = new CommunityLevelMemberAdapter(context, entityCommunityLevelMemberList);
    }

    @Override
    public void initView() {
        initActionBar(toolbar, tvTitle, "社区推广");
        rvCommunityLevelMember.setAdapter(communityLevelMemberAdapter);
        rvCommunityLevelMember.setLayoutManager(new LinearLayoutManager(context));
        communityLevelMemberAdapter.setEmptyView(R.layout.content_recyclerview_empty, (ViewGroup) rvCommunityLevelMember.getParent());
    }

    @Override
    public void initOperator() {
        JSONObject param = new JSONObject();
        try {
            param.put("level", entityCommunityLevel.getLevel() + "");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        presenter.communityLevelList(param.toString());
    }

    @Override
    public void onCommunityLevelListSuccess(List<EntityCommunityLevelMember> entityCommunityLevelMemberList) {
        super.onCommunityLevelListSuccess(entityCommunityLevelMemberList);
        this.entityCommunityLevelMemberList.clear();
        this.entityCommunityLevelMemberList.addAll(entityCommunityLevelMemberList);
        communityLevelMemberAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCommunityLevelListFail(Exception e) {
        super.onCommunityLevelListFail(e);
        ToastUtil.getInstance().shortLength(context, "" + e.getMessage());
    }
}
